package deng.ejbtx.ejb;

import javax.annotation.PostConstruct;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;

import deng.ejbtx.jboss.AppService;
import deng.pojo.SmartProperties;
import deng.pojo.SingleScriptGroovyEngine;

/**
 * Delete all incoming JMS message to a groovy script for processing.
 * 
 * @author dengz1
 *
 */
@MessageDriven(
		messageListenerInterface = MessageListener.class,
		activationConfig = { 
			@ActivationConfigProperty(					
				propertyName = "destination", propertyValue = "queue/ExampleQueue_GroovyMsgBean"),
			@ActivationConfigProperty(					
				propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
			@ActivationConfigProperty(					
				propertyName = "messageSelector", propertyValue = "") 
		})
public class GroovyMsgBean extends SingleScriptGroovyEngine implements MessageListener {
	
	@EJB
	private AppService appService;
	
	@PostConstruct
	public void init() {
		SmartProperties config = appService.getConfig();
		this.scriptFile = config.getFile("groovyMsgBeanScript");
		this.initGroovyEngine();
	}
	
	@Override
    public void onMessage(Message message) {
		logger.debug("Received message: " + message);
    	groovyVariables.setVariable("message", message);
    	runGroovyScript();
    }
}
